<?php
namespace App\Services;

use Framework\BaseService;
use GuzzleHttp\Client;

class AuthService extends BaseService
{
    /*
     * 实名认证
     */
    public function idcard($name, $idcard)
    {
        try {
            $config = config('business.idcard', '');
            $querys = 'idCard=' . $idcard . '&name=' . urlencode($name);
            $headers = [
                'Authorization' => 'APPCODE ' . $config['AppCode'],
            ];
            $url = $config['host'] . $config['path'];
            $client = new Client();
            $response = $client ->get($url, [
                'headers' => $headers,
                'query' => $querys,
            ]);
            $content = $response->getBody()->getContents();
            return [
                'code' => $response->getStatusCode(),
                'data' => $content,
            ];
        } catch (\Exception $e) {
            $this->retError('填写身份证信息有误');
        }
    }

    /*
     * 身份证正反面
     */
    public function ocr(string $path)
    {
        try {
            $config = config('business.ocr', '');
            $headers = [
                'Authorization' => 'APPCODE ' . $config['AppCode'],
            ];
            array_push($headers, 'Content-Type' . ':' . 'application/x-www-form-urlencoded; charset=UTF-8');
            $url = $config['host'] . $config['path'];
            $client = new Client();
            $response = $client->post($url, [
                'headers' => $headers,
                'form_params' => [
                    'image' => $path,
                ],
            ]);
            $content = $response->getBody()->getContents();
            return [
                'code' => $response->getStatusCode(),
                'data' => $content,
            ];
        } catch (\Exception $e) {
            $this->retError('证件照验证失败');
        }
    }

    /** 人脸活体检测
     * @param string $base64Str
     * @param string $name
     * @param string $number
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function face(string $base64Str, string $name ,string $number)
    {
        try {
            $config = config('business.face', '');
            $headers = [
                'Authorization' => 'APPCODE ' . $config['AppCode'],
            ];
            array_push($headers, 'Content-Type' . ':' . 'application/x-www-form-urlencoded; charset=UTF-8');
            $url = $config['host'] . $config['path'];
            $client = new Client();
            $response = $client->post($url, [
                'headers' => $headers,
                'form_params' => [
                    'base64Str' => $base64Str,
                    'liveChk' => 1,
                    'name' => $name,
                    'number' => $number,
                ],
            ]);
            $content = $response->getBody()->getContents();
            return [
                'code' => $response->getStatusCode(),
                'data' => $content,
            ];
        } catch (\Exception $e) {
            $this->retError('验证失败');
        }
    }


    /**营业执照识别
     * @param string $path
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function ocr_business_license(string $path)
    {
        try {
            $config = config('business.business', '');
            $headers = [
                'Authorization' => 'APPCODE ' . $config['AppCode'],
            ];
            array_push($headers, 'Content-Type' . ':' . 'application/x-www-form-urlencoded; charset=UTF-8');
            $url = $config['host'] . $config['path'];
            $client = new Client();
            $response = $client->post($url, [
                'headers' => $headers,
                'form_params' => [
                    'image' => $path,
                ],
            ]);
            $content = $response->getBody()->getContents();
            return [
                'code' => $response->getStatusCode(),
                'data' => $content,
            ];
        } catch (\Exception $e) {
            $this->retError('验证失败');
        }
    }
}
